﻿-- Function: public.workflowedit(character varying, integer, integer)

-- DROP FUNCTION public.workflowedit(character varying, integer, integer);

CREATE OR REPLACE FUNCTION public.workflowedit(
    p_bill character varying,
	p_billtype integer,
    p_billid integer,
    p_userid integer,
    p_org_id integer)
  RETURNS json AS
$BODY$
declare  
    v_status integer;---但据状态
    v_cnt integer;
    v_sql text;
    v_data text;
    v_edit boolean;
    v_check boolean;
    v_del boolean;
	v_workflow_bill_stateid integer;
begin
    v_edit := true;
    v_check := true;--是否有审批
    v_del := true;
    select COALESCE(max(id)) into v_workflow_bill_stateid 
	From public.workflow_bill_state where org_id=p_org_id and bill_enum=p_bill and billid=p_billid and bill_type=p_billtype and is_active=true;
	if v_workflow_bill_stateid =0 then
		v_check :=false;
	else
	    ---20210521添加是否可以撤回
		select count(*) into v_cnt From public.workflow_action where billid=p_billid and bill_enum=p_bill and workflow_bill_stateid=v_workflow_bill_stateid
		and id>(select COALESCE(max(id),0) from public.workflow_action where billid=p_billid and bill_enum=p_bill and status=1
			    and workflow_bill_stateid=v_workflow_bill_stateid)
		and completed_by>0;
		if v_cnt>0 then
		   v_check :=true;
		else
		   v_check :=false;
		end if;
	end if;

--------------检查但据审核状态-------------
   v_sql := 'select COALESCE(min(status),0) From public.' || p_bill || ' where  id=' || p_billid;
   execute  v_sql into v_status; 
   if v_status=0 then---
	    v_edit := true;
	    v_del :=  true;
	    v_sql :='select array_to_json(array_agg(row_to_json(t)))  from (select '|| v_edit ||' as edit, '|| v_del ||' as del, '|| v_check ||' as check,
	    false as colcontrol,''{}'' as allow_col ) t';
	    execute v_sql into v_data;
	    return v_data::json; 
   end if;
   if v_status=2 then---
	    v_edit := false;
	    v_del :=  false;
	    v_sql :='select array_to_json(array_agg(row_to_json(t)))  from (select '|| v_edit ||' as edit, '|| v_del ||' as del, '|| v_check ||' as check,
	    false as colcontrol,''{}'' as allow_col ) t';
	    execute v_sql into v_data;
	    return v_data::json; 
   end if;
   v_del :=  false;
	
      -------------------------------------检查权限
	select count(users.id) into v_cnt 
	From users where users.id=p_userid and
	groups && any(select ARRAY_AGG(id) from public.groups where name='admin');
	if v_cnt =0 then
		select count(users.id)
		 into v_cnt from 
		(select allow_user,allow_group from  public.workflow_bill_state where  id=v_workflow_bill_stateid) obj,users
		where ((users.id = any(obj.allow_user))
		or (users.groups && obj.allow_group))
		and users.id=p_userid ;
		if v_cnt =0 then
			v_edit := false;
			v_sql :='select array_to_json(array_agg(row_to_json(t)))  from (select '|| v_edit ||' as edit, '|| v_del ||' as del, '|| v_check ||' as check,
			false as colcontrol,''{}'' as allow_col ) t';
			execute v_sql into v_data;
			return v_data::json; 
		end if;
-- 		v_edit := true;
-- 		select array_to_json(array_agg(row_to_json(t))) 
-- 		 into v_data from (
-- 		select true as edit, true as del,v_check as check,obj.allow_col,obj.colcontrol
-- 		 from 
-- 		(select allow_user,allow_group,true as colcontrol,allow_col from  public.workflow_bill_state where id=v_workflow_bill_stateid) obj,users
-- 		where ((users.id = any(obj.allow_user))
-- 		or (users.groups && obj.allow_group))
-- 		and users.id=p_userid  limit 1 ) t;
	end if;
    v_edit := true;
    select array_to_json(array_agg(row_to_json(t))) 
     into v_data from (
    select true as edit, true as del,v_check as check,allow_col,true as colcontrol
     from public.workflow_bill_state where id=v_workflow_bill_stateid
      limit 1 ) t;
    return v_data::json; 
END;  
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
